var delUrl = $("#delUrl").val(),
    aoeUrl = $("#aoeUrl").val(),
    model = $("#model").val();

layui.use(['form', 'jquery', 'layer', 'dialog', 'table'], function () {
    var form = layui.form,
        layer = layui.layer,
        $ = layui.jquery,
        table = layui.table,
        dialog = layui.dialog;

    //全选
    form.on('checkbox(allChoose)', function (data) {
        var child = $(data.elem).parents('table').find('tbody input[type="checkbox"]');
        child.each(function (index, item) {
            item.checked = data.elem.checked;
        });
        form.render('checkbox');
    });
    //渲染表单
    form.render();

    table.on('toolbar(test)', function (obj) {
        cleanForm();
        var checkStatus = table.checkStatus(obj.config.id);
        switch (obj.event) {
            case 'addData':
                page('新增', aoeUrl, model);
                break;
            case 'delData':
                var data = checkStatus.data;
                var deList = [];
                data.forEach(function (n, i) {
                    deList.push(n.id);
                });
                confirm(delUrl, deList.join(), model);
                break;
        }
    });

    //监听行工具事件
    table.on('tool(test)', function (obj) {
        cleanForm();
        var data = obj.data;
        var id = obj.data.id;
        if (obj.event === 'del') {
            confirm(delUrl, id, model);
        } else if (obj.event === 'edit') {
            edit(data);
            page("修改", aoeUrl, model);
        }
    });
});

//弹出页面
function page(title, url, model, w, h) {
    if (title == null || title == '') {
        title = false;
    }
    if (url == null || url == '') {
        url = "404.html";
    }
    if (w == null || w == '') {
        w = 'auto';
    }
    if (h == null || h == '') {
        h = 'auto';
    }

    layer.open({
        title: '<span>' + title + '</span>',
        type: 1,
        area: [w, h],
        content: $(".formBox"),
        move: false,
        btnAlign: 'c',
        btn: ['确定', '取消'], //按钮
        yes: function (index) {
            var data = new FormData($("#addForm")[0]);
            $.ajax({
                url: path + url,
                type: 'POST',
                cache: false,
                data: data,
                processData: false,
                contentType: false,
                success: function (data) {
                    if (data.success) {
                        layer.alert(title + "成功");
                        layer.close(index);
                        queryList();
                    } else {
                        layer.alert(title + "失败");
                    }
                }
            })
        }
    });
}

function confirm(url, id, model, msg) {
    if (id == null || id == '') {
        return;
    }
    if (msg == null || msg == '') {
        msg = '真的删除行么';
    }
    if (url == null || url == '') {
        url = "404.html";
    }
    layer.confirm(msg, function (index) {
        $.ajax({
            url: path + url,
            type: 'POST',
            data: {"id": id},
            success: function (data) {
                if (data.success) {
                    layer.alert("删除成功");
                    layer.close(index);
                    queryList();
                } else {
                    layer.alert("删除失败");
                }
            }
        });
    });
}


function cleanForm(){
    if (model == 'category') {
        $.category.cleanForm();
    } else if (model == 'article') {
        $.article.cleanForm();
    }else if (model == 'menu') {
        $.menu.cleanForm();
    }
}

function queryList() {
    if (model == 'category') {
        $.category.queryList();
    } else if (model == 'article') {
        $.article.queryList();
    }else if(model == 'comment'){
        $.comment.queryList();
    }else if(model == 'menu'){
        $.menu.queryList();
    }
}

function edit(data) {
    if (model == 'category') {
        $.category.edit(data);
    } else if (model == 'article') {
        $.article.edit(data);
    } else if (model == 'menu') {
        $.menu.edit(data);
    }

}

/**************************************时间格式化处理************************************/
function dateFtt(fmt, date) {
    var o = {
        "M+": date.getMonth() + 1,                 //月份
        "d+": date.getDate(),                    //日
        "h+": date.getHours(),                   //小时
        "m+": date.getMinutes(),                 //分
        "s+": date.getSeconds(),                 //秒
        "q+": Math.floor((date.getMonth() + 3) / 3), //季度
        "S": date.getMilliseconds()             //毫秒
    };
    if (/(y+)/.test(fmt))
        fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt))
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}
